<?php
define('LEGAL_OPER', true);
require('../../includes/admin-init.php');
require(ROOT_PATH . '/class/utils/FileUtil.class.php');
require(ROOT_PATH . '/class/utils/ThumbUtil.class.php');
require(ROOT_PATH . '/class/utils/ValidateUtil.class.php');
require(ADMIN_PATH . '/class/CommonData.class.php');

if($method == 'storelist'){
	$id = to_int($_GET['bid']);
	$db = new DB();
	$blockTable = new BaseDao($db->getDb(), 'cms_block','block_id');
	$block = $blockTable->getById($id);
	$tablename = constant('STORE_TABLE_'.$block['block_type']);
	$storeTable = new BaseDao($db->getDb(), $tablename,'id');
	$stores = $storeTable->getAll("block_id=?", array($id),array('sort_order asc'));
	$smarty->assign("stores",$stores);
	$smarty->assign("block",$block);
	$smarty->assign("back_url",$_SERVER['HTTP_REFERER']);
	
	if($block['block_type'] == 1){
		$smarty->display("cms/store_link_list.htm");
	}else if($block['block_type'] == 2){
		$smarty->display("cms/store_img_list.htm");
	}else if($block['block_type'] == 3){
		$categorys = CommonData::getCategorys($db->getDb());
		$smarty->assign("categorys",$categorys);
		$smarty->display("cms/store_product_list.htm");
	}
	$db->close();
	exit;
}

//添加链接类型数据
if($method == 'addlink'){
	$blockid= to_int($_POST['blockid']);
	$name = $_POST['name'];
	$url = $_POST['url'];
	$title = isset($_POST['title'])?trim($_POST['title']):'';
	$sort= to_int($_POST['sort']);
	$sort = $sort>1?$sort:1;
	if($blockid > 0 && isNotEmpty($name) && isUrl($url)){
		$db = new DB();
		$db->beginTransaction();
		$storeTable = new BaseDao($db->getDb(), 'cms_store_link','id');
		try{
			$values = array();
			$values['block_id'] = $blockid;
			$values['name'] = trim($name);
			$values['href'] = trim($url);
			$values['title'] = $title;
			$values['sort_order'] = $sort;
			$storeTable->save($values);
			$id = $db->getDb()->lastInsertId();
			$db->commit();
			echo '{code:1,id:'.$id.'}';
		}catch(Exception $e){
			$db->rollback();
			echo '{code:0}';
		}
		$db->close();
	}else{
		echo '{code:0}';
	}
	exit;
}

//删除链接
if($method == 'dellink'){
	$id = to_int($_GET['id']);
	if($id>0){
		$db = new DB();
		$db->beginTransaction();
		$storeTable = new BaseDao($db->getDb(), 'cms_store_link','id');
		try{
			$storeTable->delete('id=?', array($id));
			$db->commit();
			echo 1;
		}catch(Exception $e){
			$db->rollback();
			echo 0;
		}
		$db->close();
	}else{
		echo 0;
	}
	exit;
}

//修改链接类型数据
if($method == 'editlink'){
	$id= to_int($_POST['id']);
	$name = $_POST['name'];
	$url = $_POST['url'];
	$title = isset($_POST['title'])?trim($_POST['title']):'';
	$sort= to_int($_POST['sort']);
	$sort = $sort>1?$sort:1;
	if($id > 0 && isNotEmpty($name) && isUrl($url)){
		$db = new DB();
		$db->beginTransaction();
		$storeTable = new BaseDao($db->getDb(), 'cms_store_link','id');
		try{
			$values = array();
			$values['name'] = trim($name);
			$values['href'] = trim($url);
			$values['title'] = $title;
			$values['sort_order'] = $sort;
			$storeTable->update($values, "id=?", array($id));
			$db->commit();
			echo '{code:1}';
		}catch(Exception $e){
			$db->rollback();
			echo '{code:0}';
		}
		$db->close();
	}else{
		echo '{code:0}';
	}
	exit;
}

//添加图片类型数据
if($method == 'addimg'){
	$blockid= to_int($_POST['blockid']);
	$name = $_POST['name'];
	$url = $_POST['url'];
	$title = isset($_POST['title'])?trim($_POST['title']):'';
	$sort= to_int($_POST['sort']);
	$sort = $sort>1?$sort:1;
	$isImg = ValidateUtil::checkUploadImg("image", 1000000);
	
	
	if($blockid > 0 && isNotEmpty($name) && $isImg === 1 && isUrl($url)){
		$db = new DB();
		$db->beginTransaction();
		$storeTable = new BaseDao($db->getDb(), 'cms_store_img','id');
		try{
			$values = array();
			$values['block_id'] = $blockid;
			$values['name'] = trim($name);
			$values['href'] = trim($url);
			$values['title'] = $title;
			$values['sort_order'] = $sort;
			$storeTable->save($values);
			$id = $db->getDb()->lastInsertId();
			$suffix = ThumbUtil::getSuffix($_FILES['image']['name']);
			$imgdir = '/ci/'.date('Ymd',time());
			$imgpath = $imgdir.'/'.$id.'-'.time().'.'.$suffix;
			$storeTable->update(array('src'=>$imgpath), 'id=?', array($id));
			FileUtil::mkdirs(ROOT_PATH.$imgdir);
			move_uploaded_file($_FILES['image']['tmp_name'],ROOT_PATH.$imgpath);
			$db->commit();
			echo '{code:1,id:'.$id.',src:\''.$imgpath.'\'}';
		}catch(Exception $e){
			$db->rollback();
			echo '{code:0}';
		}
		$db->close();
	}else{
		echo '{code:0}';
	}
	exit;
}

//删除图片类型数据
if($method == 'delimg'){
	$id = to_int($_GET['id']);
	if($id>0){
		$db = new DB();
		$db->beginTransaction();
		$storeTable = new BaseDao($db->getDb(), 'cms_store_img','id');
		try{
			$storeTable->delete('id=?', array($id));
			$db->commit();
			echo 1;
		}catch(Exception $e){
			$db->rollback();
			echo 0;
		}
		$db->close();
	}else{
		echo 0;
	}
	exit;
}


//修改图片类型数据
if($method == 'editimg'){
	$id= to_int($_POST['id']);
	$name = $_POST['name'];
	$url = $_POST['url'];
	$title = isset($_POST['title'])?trim($_POST['title']):'';
	$sort= to_int($_POST['sort']);
	$sort = $sort>1?$sort:1;
	if($id > 0 && isNotEmpty($name) && isUrl($url)){
		$db = new DB();
		$db->beginTransaction();
		$storeTable = new BaseDao($db->getDb(), 'cms_store_img','id');
		try{
			$values = array();
			$values['name'] = trim($name);
			$values['href'] = trim($url);
			$values['title'] = $title;
			$values['sort_order'] = $sort;
			$storeTable->update($values, "id=?", array($id));
			$db->commit();
			echo '{code:1}';
		}catch(Exception $e){
			$db->rollback();
			echo '{code:0}';
		}
		$db->close();
	}else{
		echo '{code:0}';
	}
	exit;
}

//修改图片类型数据
if($method == 'editimgsrc'){
	$id= to_int($_POST['id']);
	$isImg = ValidateUtil::checkUploadImg("image", 1000000);
	if($id > 0 && $isImg === 1){
		$db = new DB();
		$db->beginTransaction();
		$storeTable = new BaseDao($db->getDb(), 'cms_store_img','id');
		try{
			$suffix = ThumbUtil::getSuffix($_FILES['image']['name']);
			$imgdir = '/ci/'.date('Ymd',time());
			$imgpath = $imgdir.'/'.$id.'-'.time().'.'.$suffix;
			$storeTable->update(array('src'=>$imgpath), 'id=?', array($id));
			FileUtil::mkdirs(ROOT_PATH.$imgdir);
			move_uploaded_file($_FILES['image']['tmp_name'],ROOT_PATH.$imgpath);
			$db->commit();
			echo '{code:1,src:\''.$imgpath.'\'}';
		}catch(Exception $e){
			$db->rollback();
			echo '{code:0}';
		}
		$db->close();
	}else{
		echo '{code:0}';
	}
	exit;
}


//添加产品类型数据
if($method == 'addproduct'){
	$blockid= to_int($_POST['blockid']);
	$sproid= to_int($_POST['sproid']);
	$name = $_POST['name'];
	$title = isset($_POST['title'])?trim($_POST['title']):'';
	$sort= to_int($_POST['sort']);
	$sort = $sort>1?$sort:1;
	$isImg = ValidateUtil::checkUploadImg("image", 1000000);

	if($sproid > 0 && $blockid > 0 && isNotEmpty($name) && $isImg === 1){
		$db = new DB();
		$db->beginTransaction();
		$storeTable = new BaseDao($db->getDb(), 'cms_store_product','id');
		try{
			$values = array();
			$values['block_id'] = $blockid;
			$values['name'] = trim($name);
			$values['title'] = $title;
			$values['sort_order'] = $sort;
			$values['spro_id'] = $sproid;
			$storeTable->save($values);
			$id = $db->getDb()->lastInsertId();
			$suffix = ThumbUtil::getSuffix($_FILES['image']['name']);
			$imgdir = '/ci/'.date('Ymd',time());
			$imgpath = $imgdir.'/'.$id.'-'.time().'.'.$suffix;
			$storeTable->update(array('src'=>$imgpath), 'id=?', array($id));
			FileUtil::mkdirs(ROOT_PATH.$imgdir);
			move_uploaded_file($_FILES['image']['tmp_name'],ROOT_PATH.$imgpath);
			$db->commit();
			echo '{code:1,id:'.$id.',src:\''.$imgpath.'\'}';
		}catch(Exception $e){
			$db->rollback();
			echo '{code:0}';
		}
		$db->close();
	}else{
		echo '{code:0}';
	}
	exit;
}


//修改图片类型数据
if($method == 'editproductimg'){
	$id= to_int($_POST['id']);
	$isImg = ValidateUtil::checkUploadImg("image", 1000000);
	if($id > 0 && $isImg === 1){
		$db = new DB();
		$db->beginTransaction();
		$storeTable = new BaseDao($db->getDb(), 'cms_store_product','id');
		try{
			$suffix = ThumbUtil::getSuffix($_FILES['image']['name']);
			$imgdir = '/ci/'.date('Ymd',time());
			$imgpath = $imgdir.'/'.$id.'-'.time().'.'.$suffix;
			$storeTable->update(array('src'=>$imgpath), 'id=?', array($id));
			FileUtil::mkdirs(ROOT_PATH.$imgdir);
			move_uploaded_file($_FILES['image']['tmp_name'],ROOT_PATH.$imgpath);
			$db->commit();
			echo '{code:1,src:\''.$imgpath.'\'}';
		}catch(Exception $e){
			$db->rollback();
			echo '{code:0}';
		}
		$db->close();
	}else{
		echo '{code:0}';
	}
	exit;
}
//修改图片类型数据
if($method == 'editproduct'){
	$id= to_int($_POST['id']);
	$name = $_POST['name'];
	$title = isset($_POST['title'])?trim($_POST['title']):'';
	$sort= to_int($_POST['sort']);
	$sort = $sort>1?$sort:1;
	if($id > 0 && isNotEmpty($name)){
		$db = new DB();
		$db->beginTransaction();
		$storeTable = new BaseDao($db->getDb(), 'cms_store_product','id');
		try{
			$values = array();
			$values['name'] = trim($name);
			$values['title'] = $title;
			$values['sort_order'] = $sort;
			$storeTable->update($values, "id=?", array($id));
			$db->commit();
			echo '{code:1}';
		}catch(Exception $e){
			$db->rollback();
			echo '{code:0}';
		}
		$db->close();
	}else{
		echo '{code:0}';
	}
	exit;
}

//删除图片类型数据
if($method == 'delproduct'){
	$id = to_int($_GET['id']);
	if($id>0){
		$db = new DB();
		$db->beginTransaction();
		$storeTable = new BaseDao($db->getDb(), 'cms_store_product','id');
		try{
			$storeTable->delete('id=?', array($id));
			$db->commit();
			echo 1;
		}catch(Exception $e){
			$db->rollback();
			echo 0;
		}
		$db->close();
	}else{
		echo 0;
	}
	exit;
}



